from db import to_dict,to_list_dict
from fastapi import Depends,Request,WebSocket
from utils.service import ServiceRouter
from celery_tasks.tasks import add
class UserRouter(ServiceRouter):
    def __init__(self):
        super().__init__('/userinfo',data={'description':{'get':'get:获取当前信息',"post":"celery demo"}})
    async def get(self,user=Depends(ServiceRouter.auth.Certification)):
        role=(await user.role)[0]
        data=to_dict(user,fields_data=['password','create_time','_role'])
        data['role_id']=role.id
        data['name']=role.name
        return self.res(data=data)
    async def post(self,x:int,y:int):

        result=add.delay(x,y)
        return self.res(message=f"{result.get()}")
    async def websocket(self,websocket:WebSocket):
        await websocket.accept()
        try:
            while True:
                data=await websocket.receive_text()

                await websocket.send_text(f"Message text was: {data}")
        except Exception:
            await websocket.close()
            
        
